package cn.com.backstage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.*;
import cn.com.backstage.controller.base.U_labelBaseController;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE)
public class U_labelBackController extends U_labelBaseController {

	private static Logger logger = Logger.getLogger(U_labelBackController.class);

	@Autowired
	private U_labelService u_labelService;

	/**
	 * 文章标签列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "article_label_list")
	public ModelAndView article_label_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/article_label_list.jsp");
		Map map = new HashMap();
		map.put("type", 0);
		List<U_label> u_labels = u_labelService.getLabelsByMap(map);
		mv.addObject("u_labels", u_labels);
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "label-add-pre")
	public ModelAndView label_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/article_label_add.jsp");

		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			U_label u_label = u_labelService.findU_labelById(Integer.valueOf(id));
			mv.addObject("u_label", u_label);
		}
//
//		Map map = new HashMap();
//		map.put("type", 0);
//		// 获取含全名的列表
//		List<U_label> labels = u_labelService.getLabelsByMap(map);
//		for (U_label cur_label : labels) {
//			cur_label.setLabel_full_name(this.label_full_name_str(cur_label));
//		}
//		mv.addObject("labels", labels);
		return mv;
	}

	/***
	 * 返回结构 含Html
	 * 
	 * @param request
	 * @return
	 */
	public String label_full_name_str(U_label label) {
		String ret_str = " ";
		// 一级
		if (label.getParent_id() == -1) {
			ret_str = "<div class='td_cat'>" + label.getLabel_name() + "</div>";
		} else {
			U_label parent_label = u_labelService.findU_labelById(Integer.valueOf(label.getParent_id()));
			// 二级
			if (parent_label != null && parent_label.getParent_id() == -1) {
				ret_str = "<div class='td_cat'>" + parent_label.getLabel_name() + "</div><div class='td_cat'> |-- <b>"
						+ label.getLabel_name() + "</b></div>";
			}
		}
		//
		return ret_str;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "label-add")
	public void label_add(HttpServletRequest request, HttpServletResponse response, U_label u_label) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			u_labelService.addOrUpdatelabel(u_label);
			map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			map.put(Util.MESSAGE, "添加成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Util.RESULT, Util.RESULT_RC_EXCEPTION);
			map.put(Util.MESSAGE, "操作失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "getparentlabels")
	@ResponseBody
	public Map<String, Object> getparentlabels(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 所属模块
		String type = request.getParameter("type");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<U_label> list = u_labelService.getTypeParentLabels(type);
			map = new HashMap<String, Object>();
			map.put(Util.DATAS, list);
			map.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			map.put(Util.MESSAGE, "添加成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Util.RESULT, Util.RESULT_RC_EXCEPTION);
			map.put(Util.MESSAGE, "操作失败");
		}
		return map;
	}

}
